//
//  CommentIndexCell.m
//  YXFinance
//
//  Created by uplooking on 2018/10/24.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "CommentIndexCell.h"

@interface CommentIndexCell()
{
    UIImageView * imageView;
    UILabel * labelName;
    UILabel * labelTime;
    UILabel * labelTitle;
    UILabel * labelZan;
    UIImageView * rImageView;
    UIButton * button ;
    CommentIndexModel * model;
    UILabel * labelAllComment;
}
@end

@implementation CommentIndexCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self initView];
    }
    
    return self;
    
}

- (void)initView{
    
    imageView = [[UIImageView alloc]init];
    [self addSubview:imageView];
    [imageView.layer setMasksToBounds:YES];
    [imageView.layer setCornerRadius:45/2.0 * kAutoScanleX];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(45 * kAutoScanleX, 45 * kAutoScanleX));
    }];

    
    labelName = [[UILabel alloc]init];
    [labelName setTextColor:HexRGBA(0xb99c6d, 1)];
    [labelName setFont:[UIFont systemFontOfSize:13 * kAutoScanleX]];
    [labelName setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:labelName];
    [labelName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(10 * kAutoScanleX);
        make.top.equalTo(imageView).offset(5 * kAutoScanleX);
        make.height.mas_equalTo(15 * kAutoScanleX);
    }];
    
    labelTime = [[UILabel alloc]init];
    [labelTime setTextColor:HexRGBA(0xa4a4a4, 1)];
    [labelTime setFont:[UIFont systemFontOfSize:10 * kAutoScanleX]];
    [labelTime setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:labelTime];
    [labelTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(10 * kAutoScanleX);
        make.top.equalTo(labelName.mas_bottom).offset(5 * kAutoScanleX);
        make.height.mas_equalTo(12 * kAutoScanleX);
    }];

    
    
    labelZan = [[UILabel alloc]init];
    [labelZan setTextColor:HexRGBA(0x585858, 1)];
    [labelZan setFont:[UIFont systemFontOfSize:12 * kAutoScanleX]];
    [labelZan setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:labelZan];
    [labelZan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-15 * kAutoScanleX);
        make.centerY.equalTo(imageView);
    }];
    
    
    rImageView = [[UIImageView alloc]init];
    [self addSubview:rImageView];
    [rImageView setImage:[UIImage imageNamed:@"icon_dianzan"]];
    [rImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(imageView);
        make.right.equalTo(labelZan.mas_left);
        make.size.mas_equalTo(CGSizeMake(34 * kAutoScanleX, 34 * kAutoScanleX));
    }];
    
    
    
    labelTitle = [[UILabel alloc]init];
    [labelTitle setTextColor:HexRGBA(0x2e2e2e, 1)];
    [labelTitle setFont:[UIFont systemFontOfSize:14 * kAutoScanleX]];
    [labelTitle setTextAlignment:NSTextAlignmentLeft];
    [labelTitle setNumberOfLines:0];
    [self addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(10 * kAutoScanleX);
        make.right.equalTo(self).offset(-15 * kAutoScanleX);
        make.top.equalTo(labelTime.mas_bottom).offset(5 * kAutoScanleX);
    }];
    

    button = [[UIButton alloc]init];
    [button addTarget:self action:@selector(buttonZanAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(rImageView);
        make.right.equalTo(labelZan);
        make.centerY.equalTo(rImageView);
        make.height.mas_equalTo(30 * kAutoScanleX);
    }];

    
    UIView *line = [[UIView alloc]init];
    [line setBackgroundColor:HexRGBA(0xcfd4db, 1)];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    
    labelAllComment = [[UILabel alloc]init];
    [labelAllComment setHidden:YES];
    [labelAllComment setBackgroundColor:[UIColor whiteColor]];
    [labelAllComment setText:@"暂无评论~"];
    [labelAllComment setTextColor:HexRGBA(0x585858, 1)];
    [labelAllComment setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [labelAllComment setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:labelAllComment];
    [labelAllComment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}


#pragma mark -- 点赞
- (void)buttonZanAction:(UIButton *)sender{

    if (self.blockClickZan) {
        
        self.blockClickZan(model);
    }
    
}

- (void)setIndexModel:(CommentIndexModel *)indexModel{
    
    model = indexModel;
    
    if ([indexModel.msg isEqualToString:@"暂无评价"]) {
        
        [labelAllComment setHidden:NO];
        
        return;
    }
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:indexModel.avatar] placeholderImage:[UIImage imageNamed:@"icon_pinglun_touxiang"]];
    
    CGFloat height = [self heightByStringContent:indexModel.msg andLabelWidth:kScreenWidth - 85 * kAutoScanleX andFontSize:14 * kAutoScanleX]+6;
    
    [labelName setText:indexModel.nickname];
    [labelTime setText:indexModel.addtime];
    [labelTitle setText:indexModel.msg];
    [labelTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    
    [labelZan setText:[NSString stringWithFormat:@"点赞(%@)",indexModel.good]];
    
    if (indexModel.is_goods) {
        
        [button setUserInteractionEnabled:NO];
        [labelZan setText:[NSString stringWithFormat:@"已赞(%@)",indexModel.good]];
        [labelZan setTextColor:HexRGBA(0xc61d19, 1)];
        [rImageView setImage:[UIImage imageNamed:@"icon_yizan"]];
        
    }else{
        
        [button setUserInteractionEnabled:YES];
        [labelZan setText:[NSString stringWithFormat:@"点赞(%@)",indexModel.good]];
        [labelZan setTextColor:HexRGBA(0x585858, 1)];
        [rImageView setImage:[UIImage imageNamed:@"icon_dianzan"]];
    }
    
}

@end
